# --- 1. 项目基本设置 ---
cmake_minimum_required(VERSION 3.10)
project(SQLProject VERSION 1.0.0 LANGUAGES CXX)

# --- 2. C++ 标准设置 ---
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# --- 3. 平台相关的全局设置 ---
if(MSVC)
    add_compile_options("/utf-8")
    add_compile_definitions(_HAS_STD_BYTE=0 NOMINMAX)
    add_compile_options("/wd4267" "/wd4996" "/wd4715" "/wd4251")
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    add_compile_options("-finput-charset=UTF-8" "-fexec-charset=UTF-8")
endif()


# 包含项目自身的头文件目录
include_directories(
    include # 公共头文件目录
    include/db_manager
    include/db_manager/catalog
    include/logger
    include/nlohmann
    include/db_manager/utils
    include/db_manager/page
    include/db_manager/bplus_tree
    include/complier
    include/db_manager/connect_pool
    include/db_manager/protocol
    include/db_manager/conf
    include/DB-OS  # DB-OS头文件目录
    include/utils  # utils头文件目录
)

# --- 7. 收集共享源文件 ---
file(GLOB_RECURSE SHARED_SOURCES "src/*.cpp")
list(FILTER SHARED_SOURCES EXCLUDE REGEX "src/server\\.cpp$")
list(FILTER SHARED_SOURCES EXCLUDE REGEX "src/cli\\.cpp$")
list(FILTER SHARED_SOURCES EXCLUDE REGEX "src/main\\.cpp$")


# --- 8. 定义可执行文件目标 ---
add_executable(sql_server src/server.cpp ${SHARED_SOURCES})
add_executable(sql_cli src/cli.cpp ${SHARED_SOURCES})
add_executable(SQLProject src/main.cpp ${SHARED_SOURCES})


# --- 9. 目标特定的设置 ---
set(TARGETS sql_server sql_cli SQLProject)
foreach(TARGET ${TARGETS})
    if(MSVC)
        target_compile_options(${TARGET} PRIVATE /W3)
    else()
        target_compile_options(${TARGET} PRIVATE -Wall -Wextra -pedantic)
    endif()
endforeach()
# ...existing code...


if(WIN32)
    # 仅 Windows 平台需要链接 ws2_32
    target_link_libraries(sql_server ws2_32)
    target_link_libraries(sql_cli ws2_32)
    target_link_libraries(SQLProject ws2_32)
else()
    # Linux 平台无需链接 ws2_32，可链接 pthread（如果用到多线程）
    target_link_libraries(sql_server pthread)
    target_link_libraries(sql_cli pthread)
    target_link_libraries(SQLProject pthread)
endif()

# ...existing code...
# 对于GCC和Clang编译器
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    # 指定输入文件编码为UTF-8（无BOM）
    add_compile_options(-finput-charset=UTF-8)
    # 指定输出字符串编码为UTF-8
    add_compile_options(-fexec-charset=UTF-8)
endif()
# （可选）链接第三方库（以Boost为例）
# find_package(Boost 1.70 REQUIRED COMPONENTS system filesystem)
# if(Boost_FOUND)
#     include_directories(${Boost_INCLUDE_DIRS})
#     target_link_libraries(${PROJECT_NAME} PRIVATE ${Boost_LIBRARIES})
# endif()

# （可选）安装配置（指定安装路径和文件）
install(TARGETS sql_server sql_cli SQLProject
    RUNTIME DESTINATION bin
)
install(DIRECTORY include/
    DESTINATION include     # 头文件安装到include目录
)
